#!/usr/bin/env node
//必须加上上面的文件头才能运行

const fs = require("fs")
const inquirer=require("inquirer")
const path=require("path")
const ejs=require('ejs')

//询问用户问题
inquirer.prompt([
    {
        type:"input",
        name:"name",
        message:"Project name?",
        default:path.basename(process.cwd()),
    },
    {
        type:"input",
        name:"description",
        message:"description?",
        default:"Always a pleasure scaffolding your awesome static sites."
    },
    {
        type:"list",
        name:"color",
        message:"What color do you like?",
        choices:["white","black","yellow","blue","green","red","gray"]
    }
])
.then(anwsers=>{
    //模板目录
    const tmplDir=path.join(__dirname,"templates")
    //目标路径
    const destDir=process.cwd()

    console.log("tmplDir",tmplDir)
    console.log("destDir",destDir)
    //直接复制的文件
    const copyFiles=[
        "public/favicon.ico",
        "src/assets/fonts/pages.eot",
        "src/assets/fonts/pages.svg",
        "src/assets/fonts/pages.ttf",
        "src/assets/fonts/pages.woff",
        "src/assets/images/brands.svg",
        "src/assets/images/logo.png",
        "src/assets/scripts/main.js",
        "src/assets/styles/_icons.scss",
        "src/assets/styles/main.scss",
        "src/layouts/basic.html",
        "src/partials/footer.html",
        "src/partials/header.html",
        "src/partials/tags.html",
        "src/about.html",
        "src/features.html",
        "src/index.html",
        ".editorconfig",
        ".gitignore",
        ".npmrc",
        ".travis.yml",
        "gulpfile.js",
        "LICENSE",
        "README.md",
    ]
    //需要渲染的文件
    const renderFiles=[
        "src/assets/styles/_variables.scss",
        "package.json"
    ]
    //先创建好目录
    fs.mkdirSync(path.join(destDir,"public"))
    fs.mkdirSync(path.join(destDir,"src"))
    fs.mkdirSync(path.join(destDir,"src/assets"))
    fs.mkdirSync(path.join(destDir,"src/assets/fonts"))
    fs.mkdirSync(path.join(destDir,"src/assets/images"))
    fs.mkdirSync(path.join(destDir,"src/assets/scripts"))
    fs.mkdirSync(path.join(destDir,"src/assets/styles"))
    fs.mkdirSync(path.join(destDir,"src/layouts"))
    fs.mkdirSync(path.join(destDir,"src/partials"))
    //直接复制文件
    copyFiles.forEach(file=>{
        fs.copyFile(path.join(tmplDir,file),path.join(destDir,file),()=>{
            console.log("copy "+file)
        })
        
    })
    //渲染文件
    renderFiles.forEach(file=>{
        ejs.renderFile(path.join(tmplDir,file),anwsers,(err,result)=>{
            if(err) throw err
            fs.writeFileSync(path.join(destDir,file),result,()=>{
                "render "+file
            })
        })
    })

    
})